import os
from time import sleep

import allure
import pytest

import page
from page.page_login import PageLogin
from page.page_settings import PageSettings
from tool.get_driver import GetDriver
from tool.get_log import GetLogger

log = GetLogger.get_log()
module_name = str(os.path.basename(__file__)).split('.')[0]


@allure.epic("我的页面")
@allure.feature("设置测试用例")
class TestSetting:
    @classmethod
    def setup_class(cls):
        cls.driver = GetDriver.get_app_driver()
        cls.login = PageLogin(cls.driver)
        # cls.jiaming = PageJiamingceshi(cls.driver)
        # cls.task = PageTask(cls.driver)
        # cls.task.pagetask_remove_cidao_app()
        cls.setting = PageSettings(cls.driver)
        try:
            cls.login.base_click_element(page.approve)
        except:
            pass

        try:
            cls.login.base_click_element(page.skip_user_desc)
        except:
            pass
        cls.login.pagelogin(os.environ['SETTING_USERNAME'], password='123456')
        # cls.login.pagelogin('11100009222', password='123456')
        cls.login.base_click_element(page.wd_tab)
        sleep(1)
        cls.setting.pageset_click_settings()

    @classmethod
    def teardown_class(cls):
        GetDriver.quit_app_driver()

    # @pytest.mark.repeat(2)
    @allure.title("人工复验")
    @allure.story("更换头像测试用例——人工复验")
    def test01_settings_person_touxiang(self):
        self.setting.pageset_touxiang()
        try:
            log.info('断言成功')
            sleep(10)
            self.setting.base_get_screenshot_as_file(module_name, '人工验证，是否成功更换头像')
            assert True
        except:
            pass
        finally:
            self.setting.pageset_return_to_setpage_keycode()

    @allure.story("更改用户名测试用例")
    def test02_settings_person_username(self, username=os.environ['NEW_USER_NAME']):

        self.setting.pageset_new_username(username)

        try:
            assert self.setting.pageset_get_new_username() == username
            log.info('断言成功')
            self.setting.base_get_screenshot_as_file(module_name, '断言成功截图')
        except:
            log.error('断言失败')
            self.setting.base_get_screenshot_as_file(module_name, '用户名修改失败')
            raise
        finally:
            self.setting.pageset_return_to_setpage_keycode()

    @allure.story("更改性别测试用例")
    def test03_settings_person_gender(self):
        self.setting.pageset_click_person()
        self.setting.pageset_click_change_gender()
        self.setting.pageset_click_male_gender()
        try:
            sleep(3)
            assert self.setting.pageset_get_new_gender() == '男'
            log.info('断言成功')
            self.setting.base_get_screenshot_as_file(module_name, '断言成功截图')
        except:
            log.error('断言失败')
            self.setting.base_get_screenshot_as_file(module_name, '性别修改失败')
            raise
        finally:
            self.setting.pageset_return_to_setpage_keycode()

    @allure.title("人工复验")
    @allure.story("消息推送通知测试用例——人工复验")
    def test04_settings_tuisong_notice(self):
        self.setting.pageset_click_setting_notice()
        self.setting.pageset_click_app_tanchuang_tongzhi()
        try:
            log.info('断言成功')
            sleep(3)
            self.setting.base_get_screenshot_as_file(module_name, '人工验证，App弹窗通知开关是否关闭')
            assert True
        except:
            pass
        finally:
            self.setting.pageset_return_to_setpage_keycode()

    @allure.story("清理缓存")
    def test05_settings_ql_huancun(self):
        self.setting.pageset_click_ql_huancun()
        sleep(3)
        try:
            assert self.setting.pageset_get_ql_huancun_reasult() == '0 B'
            log.info('断言成功')
            self.setting.base_get_screenshot_as_file(module_name, '断言成功截图')
        except:
            log.error('断言失败')
            self.setting.base_get_screenshot_as_file(module_name, '清理缓存失败')
            raise
        finally:
            self.setting.pageset_return_to_setpage_keycode()

    @allure.story("账号安全")
    def test06_settings_zh_anquan(self):
        self.setting.pageset_click_zh_anquan()
        self.setting.pageset_click_sq_zhuxiao_zhanghao()
        try:
            assert self.setting.pageset_get_zd_wangxiao_title()
            log.info('断言成功')
            self.setting.base_get_screenshot_as_file(module_name, '断言成功截图')
        except:
            log.error('断言失败')
            self.setting.base_get_screenshot_as_file(module_name, '跳转注销页面失败')
            raise
        finally:
            self.setting.pageset_return_to_setpage_keycode()

    @allure.story("关于五十音测试用例")
    def test07_settings_about_wsy(self):
        self.setting.pageset_click_about_wsy()
        sleep(5)
        try:
            assert self.setting.base_find_element(page.about_tel)
            log.info('断言成功')
            self.setting.base_get_screenshot_as_file(module_name, '断言成功截图')
        except:
            log.error('断言失败')
            self.setting.base_get_screenshot_as_file(module_name, '进入关于五十音失败')
            raise
        finally:
            self.setting.pageset_return_to_setpage_keycode()

    @allure.story("关于五十音跳转关于早道测试用例")
    def test08_settings_about_wsy_to_zaodao(self):
        self.setting.pageset_click_about_wsy()
        self.setting.pageset_click_about_zaodao()
        sleep(5)
        try:
            assert self.setting.pageset_get_about_zaodao_title() == '关于早道'
            log.info('断言成功')
            self.setting.base_get_screenshot_as_file(module_name, '断言成功截图')
        except:
            log.error('断言失败')
            self.setting.base_get_screenshot_as_file(module_name, '进入关于早道失败')
            raise
        finally:
            self.setting.pageset_return_to_setpage_keycode()

    @allure.story("举报电话测试用例")
    def test09_settings_about_wsy_phone_num(self):
        self.setting.pageset_click_about_wsy()
        try:
            assert '400-9933-715' in self.setting.pageset_get_about_wsy_phone_num()
            log.info('断言成功')
            self.setting.base_get_screenshot_as_file(module_name, '断言成功截图')
        except:
            log.error('断言失败')
            self.setting.base_get_screenshot_as_file(module_name, '举报电话有误')
            raise
        finally:
            self.setting.pageset_return_to_setpage_keycode()
